package yuejian_07.yuejian_07.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.squareup.picasso.Picasso;



import yuejian_07.yuejian_07.R;

import yuejian_07.yuejian_07.RetrofitListener;
import yuejian_07.yuejian_07.javabean.Activities;
import yuejian_07.yuejian_07.model.ActivityModel;

import static yuejian_07.yuejian_07.base.Constant.PHOTO_URL;


public class ActInfoActivity extends Activity implements RetrofitListener<Activities> {

    TextView a_name,address,date,price,man_count,phone,intro;
    ImageView img_return,img_collect,pic;
    Button enroll;
    private int user_id;
    private int activity_id;
    private String activity_name;
    private int activity_price;
    private String activity_pic;
    private String activity_intro;
    private String activity_address;
    private String activity_date;
    private int activity_phone;
    private int activity_hot;
    private Activity activities;
    private String coresult="";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题栏
        setContentView(R.layout.activity_act_info);
        initViews();
        initEvents();
        initData();
    }

    public void initViews(){
        a_name=(TextView)findViewById(R.id.activity_name);
        address=(TextView)findViewById(R.id.address);
        date=(TextView)findViewById(R.id.date);
        price=(TextView)findViewById(R.id.price);
        man_count=(TextView)findViewById(R.id.man_count);
        phone=(TextView)findViewById(R.id.phone);
        intro=(TextView)findViewById(R.id.info);
        img_return=(ImageView) findViewById(R.id.img_return);
        img_collect=(ImageView) findViewById(R.id.collect);
        pic=(ImageView) findViewById(R.id.pic);
        enroll=(Button) findViewById(R.id.enroll);
    }

    public void initEvents(){

        img_return.setOnClickListener(new View.OnClickListener() {//返回
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        img_collect.setOnClickListener(new View.OnClickListener() {//收藏
            @Override
            public void onClick(View v) {
                //collectActivity();
            }
        });

        enroll.setOnClickListener(new View.OnClickListener(){//购买跳转
            @Override
            public void onClick(View v){
                Intent intent = new Intent(ActInfoActivity.this, EnrollActActivity.class);
                Bundle bundle  = new Bundle();
                bundle.putString("activity_name",activity_name);
                bundle.putInt("activity_price",activity_price);
                bundle.putInt("activity_id",activity_id);
                bundle.putString("activity_intro",activity_intro);
                //bundle.putInt("user_id",user_id);
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });

    }

    public void initData(){

        getActivityInfo();
        getUserId();
        //checkCollect();

    }

    //获取菜品信息数据
    public void getActivityInfo(){

        Bundle bundle = getIntent().getExtras();
        activity_id = bundle.getInt("activity_id");
        ActivityModel activityModel = new ActivityModel();
        activityModel.getActivityById(activity_id,this);

    }

    public void onSuccess(Activities activities){
        activity_name = activities.getActivity_name();
        activity_price = activities.getActivity_price();
        activity_pic =PHOTO_URL+activities.getActivity_pic();
        activity_intro=activities.getActivity_intro();
        activity_address=activities.getActivity_address();
        activity_date=activities.getActivity_time();
        activity_phone=activities.getActivity_phone();
        activity_hot=activities.getActivity_hot();
        a_name.setText(activity_name);
        price.setText("￥" + activity_price);
        intro.setText(activity_intro);
        address.setText(activity_address);
        date.setText(activity_date);
        phone.setText(activity_phone+"");
        man_count.setText(activity_hot+"");
        if (activity_pic != "") {
            Picasso.with(ActInfoActivity.this)
                    .load(activity_pic)
                    .into(pic);
            pic.setScaleType(ImageView.ScaleType.FIT_XY);
        } else {
            pic.setImageResource(R.drawable.no_pic);
        }

    }

    public void onFail(){ Toast.makeText(ActInfoActivity.this, "网络发生错误", Toast.LENGTH_SHORT).show();}

    //获取个人用户信息
    public void getUserId(){
        SharedPreferences sharedPreferences = getSharedPreferences("userInfo", Context.MODE_PRIVATE);
        user_id = sharedPreferences.getInt("user_id", 0);
    }


}
